package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class ProjectProductService {

	@Autowired
	private ProjectProductDao projectProductDao;

	private QProjectProduct $ = QProjectProduct.projectProduct;

	@Autowired
	public void setProjectProductDao(ProjectProductDao projectProductDao) {
		this.projectProductDao = projectProductDao;
	}

	public List<ProjectProduct> findAll() {
		return ListUtil.toList(projectProductDao.findAll());
	}

	public List<ProjectProduct> findAll(Long projectId, Long productId,
			String batchNo, String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(projectId)) {
			condition.and($.project.id.eq(projectId));
		}

		if (IdUtil.isNotEmpty(productId)) {
			condition.and($.product.id.eq(productId));
		}

		if (StringUtil.isNotEmpty(batchNo)) {
			condition.and($.batchNo.contains(batchNo));
		}

		return ListUtil.toList(projectProductDao.findAll(condition));
	}

	public Page<ProjectProduct> findAll(Long projectId, String productName,
			Long productId, String batchNo, int pageNumber, int pageSize,
			String sortAttribute, String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(projectId)) {
			condition.and($.project.id.eq(projectId));
		}
		if (IdUtil.isNotEmpty(productId)) {
			condition.and($.product.id.eq(productId));
		}
		if (StringUtil.isNotEmpty(batchNo)) {
			condition.and($.batchNo.contains(batchNo));
		}

		if(StringUtil.isNotEmpty(productName)){
			condition.and(this.$.product.name.contains(productName));
		}
		
		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return projectProductDao.findAll(condition, pageRequest);
	}

	public ProjectProduct getProjectProduct(Long id) {
		return projectProductDao.getOne(id);
	}

	public void deleteProjectProduct(Long id) {
		projectProductDao.delete(id);
	}

	public void saveProjectProduct(ProjectProduct projectProduct) {
		projectProductDao.save(projectProduct);
	}

	public List<ProjectProduct> findByProjectId(Long projectId) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(projectId)) {
			condition.and($.project.id.eq(projectId));
		}

		return ListUtil.toList(projectProductDao.findAll(condition));
	}

	public ProjectProduct findOne(Long id) {
		return this.projectProductDao.findOne(id);
	}

}
